﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Openfree.Framework.DataAccess;

public partial class SiteAdmin_TagStatistic : SiteAdminBase
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            string cmdText = "select * from T_TagStatistic order by SumCount desc";
            FillGirdView(0, cmdText);
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }

    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        ViewState[this.ClientID + "cmdText"] = cmdText;
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    public void searchFree_OnQueryData(SearchEventArgs e)
    {
        string cmdText = @"select * from T_TagStatistic {0} order by SumCount";
        if (e.FilterString != string.Empty)
        {
            cmdText = string.Format(cmdText, " where " + e.FilterString);
        }
        else
        {
            cmdText = string.Format(cmdText, string.Empty);
        }
        FillGirdView(0, cmdText);
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["TagID"]))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        string id = Request["TagID"].Replace("'", string.Empty);
        id = id.Replace(",", "','");
        

        string cmdText = "insert into T_TagUnpopular(TagID, Subject, Category) select TagID, Subject, Category from T_TagStatistic where TagID in ('{0}')";
        BaseAccess.ExecuteNonQuery(base.Connection,
            string.Format(cmdText, id));
        cmdText = "delete from T_TagStatistic where TagID in ('{0}')";
        BaseAccess.ExecuteNonQuery(base.Connection,
            string.Format(cmdText, id));
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }    
}
